import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import '../../app_files.dart';
import 'action.dart';
import 'state.dart';
import 'dart:io';

Effect<EngineerOrderViewState> buildEffect() {
  return combineEffects(<Object, Effect<EngineerOrderViewState>>{
    EngineerOrderViewAction.onTapAddPic: _onTapAddPic,
    EngineerOrderViewAction.onTapPic: _onTapPic,
    EngineerOrderViewAction.onTapAddPic2: _onTapAddPic2,
    EngineerOrderViewAction.onTapPic2: _onTapPic2,
    EngineerOrderViewAction.onSubmit: _onSubmit,
    Lifecycle.initState: _initState,
  });
}

void _onAction(Action action, Context<EngineerOrderViewState> ctx) {}

void _initState(Action action, Context<EngineerOrderViewState> ctx) async {
  bus.emit('updateUserPosition');
  ResultData res = await CommonNetWork.fixOrderDetail(ctx.state.model.itemID);
  if (res.success) {
    String jsonContent =
        CommonUtils.checkNullToString(res.data['data']['bizMaintenanceremark']);
    Map info;
    try {
      if (jsonContent.isNotEmpty) {
        info = jsonDecode(jsonContent);
      }
    } catch (e) {
      info = null;
    }
    if (info != null) {
      ctx.state.contentController.text = info['RepairContent'];
      ctx.state.contentBeforeController.text = info['BeforeContent'] ?? '';
      ctx.state.imageList.addAll(info['RepairPhotos'] ?? []);
      ctx.state.imageBeforeList.addAll(info['BeforePhotos'] ?? []);
      List orderPhoto =
          CommonUtils.checkNullToString(res.data['data']['bizMaintenancefiles'])
                  .isEmpty
              ? []
              : CommonUtils.checkNullToString(
                      res.data['data']['bizMaintenancefiles'])
                  .split(',');
      ctx.state.orderPhoto.addAll(orderPhoto);
      if (CommonUtils.checkNullToString(res.data['data']['bizSignature']).isNotEmpty) {
        ctx.state.signPhoto =
            CommonUtils.checkNullToString(res.data['data']['bizSignature']);
      }
      ctx.dispatch(EngineerOrderViewActionCreator.onAction());
    }
  }
}

void _onTapAddPic(Action action, Context<EngineerOrderViewState> ctx) async {
  AssetEntityFile imgFile = await CommonUtils.showOrderImagePicker(ctx.context,
      type: 2, info: ctx.state.model);
  if (imgFile != null) {
    ctx.state.imageList.add(imgFile);
    ctx.dispatch(EngineerOrderViewActionCreator.onAction());
  }
}

void _onTapPic(Action action, Context<EngineerOrderViewState> ctx) async {
  var file = action.payload;
  int res =
  await CommonUtils.showActionSheet(ctx.context, button: ['查看', '删除']);
  if (res == 1) {
    ctx.state.imageList.remove(file);
    ctx.dispatch(EngineerOrderViewActionCreator.onAction());
  } else if (res == 0) {
    await CommonUtils.showMediaAlbumView(
      ctx.context,
      initialIndex: ctx.state.imageList.indexOf(file),
      files: ctx.state.imageList,
    );
  }
}

void _onTapAddPic2(Action action, Context<EngineerOrderViewState> ctx) async {
  AssetEntityFile imgFile = await CommonUtils.showOrderImagePicker(ctx.context,
      type: 2, info: ctx.state.model);
  if (imgFile != null) {
    ctx.state.imageBeforeList.add(imgFile);
    ctx.dispatch(EngineerOrderViewActionCreator.onAction());
  }
}

void _onTapPic2(Action action, Context<EngineerOrderViewState> ctx) async {
  var file = action.payload;
  int res =
  await CommonUtils.showActionSheet(ctx.context, button: ['查看', '删除']);
  if (res == 1) {
    ctx.state.imageBeforeList.remove(file);
    ctx.dispatch(EngineerOrderViewActionCreator.onAction());
  } else if (res == 0) {
    await CommonUtils.showMediaAlbumView(
      ctx.context,
      initialIndex: ctx.state.imageBeforeList.indexOf(file),
      files: ctx.state.imageBeforeList,
    );
  }
}

void _onSubmit(Action action, Context<EngineerOrderViewState> ctx) async {
  if (ctx.state.contentController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请填写报装内容');
    return;
  }
  if (ctx.state.contentBeforeController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请填写报装前内容');
    return;
  }
  CommonUtils.pushRoute(ctx.context, RoutePath.engineerResultView, arguments: {
    'isDetail': false,
    'repairContent': ctx.state.contentController.text,
    'repairPhotos': ctx.state.imageList,
    'beforePhotos': ctx.state.imageBeforeList,
    'beforeContent': ctx.state.contentBeforeController.text,
    'signPhoto': ctx.state.signPhoto,
    'orderPhotos': ctx.state.orderPhoto,
    'model': ctx.state.model
  });
}
